package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.mess.ArticleVisitStreamMess;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 热点文章分值实时更新 处理器
 */
@Component
@Slf4j
public class HotArticleScoreUpdateHandlerListener {

    @Autowired
    ApArticleService apArticleService;

    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_INCR_HANDLE_TOPIC)
    public void msg(String msg){
        log.info("HotArticleScoreUpdateHandlerListener 接受到消息  {}", msg);
        //重新计算文章分值  跟新redis
        if(StringUtils.isNotBlank(msg)){
            ArticleVisitStreamMess mess = JSON.parseObject(msg, ArticleVisitStreamMess.class);
            apArticleService.updateArticleScore(mess);
        }

    }

}
